Ext.define('wmf.controller.product.product', {
    extend: 'Ext.app.Controller',
    stores: ['product.product',
             'product.product1',
             'product.serviceType',
             'product.needAudit',
             'product.priority',
             'product.channel1',
             'product.channel2',
             'product.channel'
    ],
    models: ['product.product'],
    views: ['product.main', 
            'product.list',
            'product.add',
            'product.edit',
            'product.query',
            'product.operation',
            'product.east'
    ],
    
    refs: [{
        ref: 'productList',
        selector: 'productList'
    },{
    	ref: 'productMain',
        selector: 'productMain'
    },{
        ref: 'productQuery',
        selector: 'productQuery'
    },{
        ref: 'productAdd',
        selector: 'productAdd'
    },{
        ref: 'productEdit',
        selector: 'productEdit'
    },{
        ref: 'productOperation',
        selector: 'productOperation'
	},{
        ref: 'productEast',
        selector: 'productEast'
    }],
    
    init: function () {
        this.control({
        	'productList': {
        		beforerender : this.onBeforeRender,
        		celldblclick: this.onCellDblClick
            },
            'productList button#add': {
                click: this.onAddButtonClick
            },
            'productList button#query': {
                click: this.onQueryButtonClick
            },
            'productList button#on': {
                click: this.onOnButtonClick
            },
            'productList button#off': {
                click: this.onOffButtonClick
            },
            'productList button#operation': {
                click: this.onOperationButtonClick
            },
            'productList button#export': {
                click: this.onExportButtonClick
            },
			'productEast tool#minimize': {
				click: this.onMinToolClick
			},
			'productAdd button#save': {
				click: this.onAddSaveButtonClick
			},
			'productAdd button#reset': {
				click: this.onAddResetButtonClick
			},
			'productAdd button#cancel': {
				click: this.onMinToolClick
			},
			'productEdit button#save': {
				click: this.onEditSaveButtonClick
			},
			'productEdit button#reset': {
				click: this.onEditResetButtonClick
			},
			'productEdit button#cancel': {
				click: this.onMinToolClick
			},
			'productOperation button#save': {
				click: this.onOperationSaveButtonClick
			},
			'productOperation button#cancel': {
				click: this.onMinToolClick
			},
        	'productQuery button#save': {
				click: this.onQuerySaveButtonClick
			},
			'productQuery button#reset': {
				click: this.onQueryResetButtonClick
			},
			'productQuery button#cancel': {
				click: this.onMinToolClick
			}
        });
        wmf.product = {};
        wmf.product.params = {};
        wmf.product.data = {};
        wmf.product.init = true;
    },

    onAddButtonClick: function(){
    	var win = this.getProductEast();
        win.removeAll();
        win.add(Ext.widget("productAdd"));
        win.show();
    },
    
    onCellDblClick: function(list, td, cellIndex, record, tr, rowIndex, e, eOpts){
    	var win = this.getProductEast();
        win.removeAll();
        win.add(Ext.widget("productEdit"));
        win.show();
        wmf.product.data = record.data;
        this.getProductEdit().getForm().setValues(wmf.product.data);
    },
    
    onQueryButtonClick: function(){
    	var win = this.getProductEast();
        win.removeAll();
        win.add(Ext.widget("productQuery"));
        win.show();
        this.getProductQuery().getForm().setValues(wmf.product.params);
    },
    
    onOnButtonClick: function(){
    	var me = this;
    	var select = me.getProductList().getSelectionModel().getSelection();
    	if(select.length == 0){
    		Ext.Msg.alert('提示', '请先选择产品');
			return false;
    	}
    	var productIds = '';
    	for(var i = 0; i < select.length; i++){
    		var data = select[i].data;
    		if(data.status != '0'){
    			Ext.Msg.alert('提示', '请选择已下线的产品');
    			return false;
    		}
    		productIds += data.id + ',';
    	}
    	productIds = productIds.substring(0, productIds.length - 1);
    	Ext.Ajax.request({
			url:'product/changeProductStatus.do',
			params: {
				productIds: productIds,
				status: 1
			},
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				me.getProductList().getSelectionModel().clearSelections();
				me.getProductList().getStore().reload();
				Ext.Msg.alert("提示", "上线成功");
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onOffButtonClick: function(){
    	var me = this;
    	var select = me.getProductList().getSelectionModel().getSelection();
    	if(select.length == 0){
    		Ext.Msg.alert('提示', '请先选择产品');
			return false;
    	}
    	var productIds = '';
    	for(var i = 0; i < select.length; i++){
    		var data = select[i].data;
    		if(data.status != '1'){
    			Ext.Msg.alert('提示', '请选择已上线的产品');
    			return false;
    		}
    		productIds += data.id + ',';
    	}
    	productIds = productIds.substring(0, productIds.length - 1);
    	Ext.Ajax.request({
			url:'product/changeProductStatus.do',
			params: {
				productIds: productIds,
				status: 0
			},
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				me.getProductList().getSelectionModel().clearSelections();
				me.getProductList().getStore().reload();
				Ext.Msg.alert("提示", "下线成功");
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onOperationButtonClick: function(){
    	var me = this;
    	var select = me.getProductList().getSelectionModel().getSelection();
    	if(select.length == 0){
    		Ext.Msg.alert('提示', '请先选择产品');
			return false;
    	}
    	var productIds = '';
    	for(var i = 0; i < select.length; i++){
    		productIds += select[i].data.id + ',';
    	}
    	productIds = productIds.substring(0, productIds.length - 1);
    	var win = this.getProductEast();
        win.removeAll();
        win.add(Ext.widget("productOperation"));
        win.show();
        me.getProductOperation().getForm().setValues({"productIds": productIds});
    },
    
    onExportButtonClick: function(){
    	var id = wmf.product.params.id ? wmf.product.params.id : '';
		var productName = wmf.product.params.productName ? wmf.product.params.productName : '';
		var ecId = wmf.product.params.ecId ? wmf.product.params.ecId : '';
		window.location.href='product/exportProductList.do?id='
			+encodeURI(id)+'&productName='
			+encodeURI(productName)+'&ecId='
			+encodeURI(ecId);
    },
    
    onExportButtonClick1: function(){
    	var shopId = wmf.product.params.shopId;
		var startTime = wmf.product.params.startTime;
		var endTime = wmf.product.params.endTime;
		window.location.href='product/exportproduct.do?shopId='
			+encodeURI(shopId)+'&startTime='
			+encodeURI(startTime)+'&endTime='
			+encodeURI(endTime);
    },

    onAddResetButtonClick: function(){
    	this.getProductAdd().getForm().reset();
    },
    
    onCheckSaveChannel: function(value){
    	var serviceType = value.serviceType;
    	if(serviceType == '2'){
    		if(value.unicomChannelId || value.telcomChannelId){
    			Ext.Msg.alert('提示', '只能选择移动通道！');
    			return false;
    		}
    	}else if(serviceType == '3'){
    		if(value.cmccChannelId || value.telcomChannelId){
    			Ext.Msg.alert('提示', '只能选择联通通道！');
    			return false;
    		}
    	}else if(serviceType == '4'){
    		if(value.unicomChannelId || value.cmccChannelId){
    			Ext.Msg.alert('提示', '只能选择电信通道！');
    			return false;
    		}
    	}else if(serviceType == '5'){
    		if(value.cmccChannelId){
    			Ext.Msg.alert('提示', '只能选择联通、电信通道！');
    			return false;
    		}
    	}else if(serviceType == '6'){
    		if(value.telcomChannelId){
    			Ext.Msg.alert('提示', '只能选择移动、联通通道！');
    			return false;
    		}
    	}else if(serviceType == '7'){
    		if(value.unicomChannelId){
    			Ext.Msg.alert('提示', '只能选择移动、电信通道！');
    			return false;
    		}
    	}
    	return true;
    },
    
	onAddSaveButtonClick: function(){
    	var me = this;
    	var former = this.getProductAdd();
    	var value = former.getValues();
    	if(!me.onCheckSaveChannel(value)){
    		return false
    	}
		var win = this.getProductEast();
		Ext.Ajax.request({
			url:'product/saveProduct.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getProductList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onEditResetButtonClick: function(){
    	this.getProductEdit().getForm().setValues(wmf.product.data);
    },
    
	onEditSaveButtonClick: function(){
    	var me = this;
    	var former = this.getProductEdit();
    	var value = former.getValues();
    	if(!me.onCheckSaveChannel(value)){
    		return false
    	}
		var win = this.getProductEast();
		Ext.Ajax.request({
			url:'product/saveProduct.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getProductList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onOperationSaveButtonClick: function(){
    	var me = this;
    	var former = this.getProductOperation();
    	var value = former.getValues();
		var win = this.getProductEast();
		Ext.Ajax.request({
			url:'product/batchEditProducts.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getProductList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onQueryResetButtonClick: function(){
    	var form = this.getProductQuery().getForm();
    	form.reset();
    },
    
	onQuerySaveButtonClick: function(){
    	var me = this;
    	var former = this.getProductQuery();
    	var value = former.getValues();
		var win = this.getProductEast();
		wmf.product.init = true;
		wmf.product.params = value;
		win.hide();
		me.getProductList().getStore().pagebar.getStore().currentPage = 1;
		me.getProductList().getStore().reload();
    },
	
	onMinToolClick: function(){
    	var win = this.getProductEast();
    	win.hide();
    },
    
    onBeforeRender: function(component,options){
    	wmf.data.loadAction(this.getProductMain().itemId, this.getProductList());
    	wmf.product.params = {};
    	component.getStore().load();
    }
});